public class BinarySearchTree {
    static class TreeNode{
        public int val;
        public TreeNode left;
        public TreeNode right;

        public TreeNode(int val){
            this.val = val;
        }
    }

    public TreeNode root;

    /***
     * 时间复杂度: 最好情况:O(logN)
     *            最坏情况:O(N)
     * @param val
     */

    public void insert(int val){
        if(root == null){
            root = new TreeNode(val);
            return;
        }
        TreeNode parent = null;
        TreeNode cur = root;
        TreeNode node = new TreeNode(val);
        while(cur != null){
            // parent = cur
            if(cur.val < val){
                parent = cur;
                cur = cur.right;

            }else if(cur.val > val){
                parent = cur;
                cur = cur.left;
            }else{
                //不能插入两个相同的元素
                return;
            }
        }
        if (parent.val > val){
            parent.left = node;
        }else{
            parent.right = node;
        }
    }

    public TreeNode search(int key){
        TreeNode cur = root;
        while(cur != null){
            if(cur.val == key){
                return root;
            }else if(cur.val > key){
                cur = cur.left;
            }else{
                cur = cur.right;
            }
        }
        return null;
    }

    public void remove(int key){
        if(root == null){
            return;
        }
        TreeNode parent = null;
        TreeNode cur = root;
        while(cur != null){
            // parent = cur
            if(cur.val < key){
                parent = cur;
                cur = cur.right;

            }else if(cur.val > key){
                parent = cur;
                cur = cur.left;
            }else{
                removeNode(parent,cur);
            }
        }
    }

    private void removeNode(TreeNode parent,TreeNode cur){
        if(cur.left == null){
            if(cur == root){
                root = cur.right;
            }else if(parent.left == cur){
                parent.left = cur.right;
            }else {
                parent.right = cur.right;
            }
        }else if(cur.right == null){
            if(cur == root){
                root = cur.left;
            }else if(parent.left == cur){
                parent.left = cur.left;
            }else {
                parent.right = cur.left;
            }
        }else {
            TreeNode target = cur.right;
            TreeNode targetParent = cur;
            while(target.left != null) {
                targetParent = target;
                target = target.left;
            }
            cur.val = target.val;
            if(targetParent.left != target){
                targetParent.right = target.right;
            }else {
                targetParent.left = target.right;
            }


            // TreeNode target2 = cur.left;

/*             TreeNode targetParent2 = cur;
            while(target2.right != null {
                targetParent2 = target2;
                target2 = target2.right;
            } */

            // TreeNode target = (target1.val > target2.val ? )
        }
    }
}
